var redis = require("redis")
var handler = module.exports
var RDS_PORT = 6379
var RDS_HOST = "127.0.0.1"
var RDS_PWD = "MyRedis2018"
var RDS_OPTS = {auth_pass : RDS_PWD}
var ROOM_ALL_AMOUNT = 20000			   //总房间数量
var ROOM_BEGIN_INDEX = 200800   	   //起始房间ID
handler.start = function(dbService) {
	var db = redis.createClient(RDS_PORT,RDS_HOST,RDS_OPTS)
	db.on("ready",function(res) {
		db.select("0",function(err) {
			dbService.db = db
			//数据库初始配置
			dbService.db.get("nn:acc:lastid",function(err,data) {
				if(data === null){
			        console.log("\033[33m[INFO] DataBase check - nn:acc:lastid\033[0m");
			        db.set("nn:acc:lastid",10000);
	    		}
			})
			dbService.db.get("nn:acc:addDiamond",function(err,data) {
				if(data === null){
			        console.log("\033[33m[INFO] DataBase check - nn:acc:addDiamond\033[0m");
			        db.set("nn:acc:addDiamond",0);
	    		}
			})
			dbService.db.get("nn:notifys",function(err,data) {
				if(data === null){
			        console.log("\033[33m[INFO] DataBase check - nn:notifys\033[0m");
			        var notify = {"1" : {"name" : "","content" : ""}}
			        db.set("nn:notifys",JSON.stringify(notify));
	    		}
			})
			//游戏类型开关
			dbService.db.get("nn:game:switch",function(err,data) {
				if(data === null){
			        console.log("\033[33m[INFO] DataBase check - nn:game:switch\033[0m");
			        var tmpTable = {
			        	"niuniu" : true,
			        	"zhajinniu" : false,
			        	"mingpaiqz" : true,
			        	"fengkuang" : false,
			        	"sanKung" : true,
			        	"zhajinhua" : true
			        }
			        db.set("nn:game:switch",JSON.stringify(tmpTable));
	    		}
			})


			//gameServer数据
			//roomList
			dbService.db.hexists("gameServer:roomList","flag",function(err,data) {
				if(err || !data){
					dbService.db.hset("gameServer:roomList","flag",true)
	    		}
			})
			//userMap
			dbService.db.hexists("gameServer:userMap","flag",function(err,data) {
				if(err || !data){
					dbService.db.hset("gameServer:userMap","flag",true)
	    		}
			})
			//roomMap
			dbService.db.hexists("gameServer:RoomMap","flag",function(err,data) {
				if(err || !data){
					dbService.db.hset("gameServer:RoomMap","flag",true)
	    		}
			})
			//agencyList
			dbService.db.hexists("gameServer:agencyList","flag",function(err,data) {
				if(err || !data){
					dbService.db.hset("gameServer:agencyList","flag",true)
	    		}
			})
			//AgencyReopenList
			dbService.db.hexists("gameServer:AgencyReopenList","flag",function(err,data) {
				if(err || !data){
					dbService.db.hset("gameServer:AgencyReopenList","flag",true)
	    		}
			})
			//roomHostList
			dbService.db.hexists("gameServer:roomHostList","flag",function(err,data) {
				if(err || !data){
					dbService.db.hset("gameServer:roomHostList","flag",true)
	    		}
			})
			//抽奖次数表
			dbService.db.hexists("gameServer:lotteryCountList","flag",function(err,data) {
				if(err || !data){
					dbService.db.hset("gameServer:lotteryCountList","flag",true)
	    		}
			})
			//获奖列表   gameServer:lotteryAwardList
			//抽奖记录   gameServer:lotteryRecordList
			//重要物品记录表 gameServer:importanceAwardList
			//抽奖配置表
			dbService.db.exists("gameServer:lotteryTable",function(err,data) {
				if(err || !data){
					var info = {
						"0" : JSON.stringify({
							"itemId" : 0,
							"itemName" : "",
							"title" : "再接再厉",
							"count" : 0,
							"weight" : 1000
						}),
						"1" : JSON.stringify({
							"itemId" : 1002,
							"itemName" : "iphoneX",
							"title" : "iphoneX",
							"count" : 1,
							"weight" : 1,
							"importance" : true
						}),
						"2" : JSON.stringify({
							"itemId" : 101,
							"itemName" : "房卡",
							"title" : "房卡2张",
							"count" : 2,
							"weight" : 1000
						}),
						"3" : JSON.stringify({
							"itemId" : 1003,
							"itemName" : "imac",
							"title" : "imac",
							"count" : 1,
							"weight" : 1,
							"importance" : true
						}),
						"4" : JSON.stringify({
							"itemId" : 1001,
							"itemName" : "话费",
							"title" : "话费10元",
							"count" : 10,
							"weight" : 1
						}),
						"5" : JSON.stringify({
							"itemId" : 1004,
							"itemName" : "ipad air",
							"title" : "ipad air",
							"count" : 1,
							"weight" : 1,
							"importance" : true
						}),
						"6" : JSON.stringify({
							"itemId" : 101,
							"itemName" : "房卡",
							"title" : "房卡1张",
							"count" : 1,
							"weight" : 1000
						}),
						"7" : JSON.stringify({
							"itemId" : 1001,
							"itemName" : "话费",
							"title" : "话费20元",
							"count" : 20,
							"weight" : 1
						}),
						"8" : JSON.stringify({
							"itemId" : 0,
							"itemName" : "",
							"title" : "再接再厉",
							"count" : 0,
							"weight" : 1000
						}),
						"9" : JSON.stringify({
							"itemId" : 101,
							"itemName" : "房卡",
							"title" : "房卡1张",
							"count" : 1,
							"weight" : 1000
						})
					}
					dbService.db.hmset("gameServer:lotteryTable",info)
	    		}
			})
			//抽奖库存表
			dbService.db.exists("gameServer:lotteryInventory",function(err,data) {
				if(err || !data){
					var info = {
						"1001" : 100,
						"1002" : 100,
						"1003" : 100,
						"1004" : 100
					}
					dbService.db.hmset("gameServer:lotteryInventory",info)
				}
	    	})

			//茶楼相关
			dbService.db.get("teaHouse:lastid",function(err,data) {
				if(data === null){
			        console.log("\033[33m[INFO] DataBase check - teaHouse:lastid\033[0m");
			        db.set("teaHouse:lastid",168888);
	    		}
			})
			dbService.db.hgetall("config:tea",function(err,data) {
				if(err || !data){
					var cfg = {
						"open" : 100,
						"table1" : 100,
						"table2" : 100,
						"table3" : 100,
						"table4" : 100,
						"table5" : 100,
						"table6" : 100,
						"table7" : 100,
						"table8" : 100,
						"defaultCount" : 4
					}
					dbService.db.hmset("config:tea",cfg,function(err) {})
				}
			})
		})
	})
}